package com.itbaima.csmall.cart.webapi.controller;

import com.itbaima.csmall.cart.service.CartService;
import com.itbaima.csmall.commons.pojo.cart.dto.CartAddDTO;
import com.itbaima.csmall.commons.restful.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * <p>CartController</p>
 *
 * @author Chen
 * @since 2023/2/20 12:31:31
 */
@Slf4j
@RestController
@RequestMapping("/base/cart")
@Api(tags = "购物车管理模块")
public class CartController {

    // 装配业务逻辑层对象
    @Autowired
    private CartService cartService;
    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/add")
    @ApiOperation("新增购物车中的商品")
    public JsonResult<Void> cartAdd(CartAddDTO cartAddDTO) {
        cartService.cartAdd(cartAddDTO);
        return JsonResult.ok("新增购物车商品完成!");
    }

    @PostMapping("/delete")
    @ApiOperation("删除购物车中的商品")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户Id", name = "userId", example = "UU100"),
            @ApiImplicitParam(value = "商品编号", name = "commodityCode", example = "PC100"),
    })
    public JsonResult<Void> deleteUserCart(String userId, String commodityCode) {
        String url = "http://localhost:20003/base/stock/reduce/count?commodityCode={1}&reduceCount={2}";
        JsonResult jsonResult = restTemplate.getForObject(url, JsonResult.class, commodityCode, 5);
        log.info("rest调用完毕,返回值:{}", jsonResult);
        cartService.deleteUserCart(userId, commodityCode);
        return JsonResult.ok("删除购物车商品完成");
    }

}
